/*
 * ============================================================================
 * Copyright Notice!
 * ----------------------------------------------------------------------------
 * This file contains proprietary and confidential information belonging to the
 * group of people involvled in developing this project.  Reproduction, disclosure 
 * or use without specific written authorization from project stake holders is 
 * prohibited. Please refer the readme.txt in project installation folder for more
 * specific contacts
 *
 * All rights reserved.
 * ============================================================================
 */
package edu.uwm.universitydb.dao.project;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.uwm.universitydb.model.Project;
import edu.uwm.universitydb.service.project.ProjectSortOrderEnum;

/**
 * <code>SearchProjectByDepartmentCommand</type>
 * 
 * @author Shyam Nair
 */
public class SearchProjectByDepartmentCommand extends SearchProjectCommandBase<List<Project>>
{
    /** static String representing the where clause of SQL */
    private static final String SQL_WHERE_CLAUSE = "WHERE dept.dno = ? AND "
        + "university.works.dno = dept.dno AND "
        + "university.works.prof_ssn = prof.prof_ssn AND "
        + "prof.prof_ssn = university.manage.prof_ssn AND "
        + "university.manage.pno = p.pno";

    /** attribute holding the sort order */
    private final ProjectSortOrderEnum mSortOrder;

    /** attribute holding the ssn */
    private final String mDeptId;

    /**
     * 
     * @param deptId
     * @param sortOrder
     */
    public SearchProjectByDepartmentCommand(final String deptId,
                                            final ProjectSortOrderEnum sortOrder)
    {
        mSortOrder = sortOrder;
        mDeptId = deptId;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DataFetchCommand#processResult(java.sql.ResultSet)
     */
    @Override
    public List<Project> processResult(final ResultSet result) throws SQLException
    {
        final List<Project> projectList = new ArrayList<Project>();
        while (result.next())
        {
            final Project project = getProject(result);
            projectList.add(project);
        }
        return projectList;
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DBCommand#getSQL()
     */
    @Override
    public String getSQL()
    {
        final StringBuilder sql = new StringBuilder(SELECT_SQL);
        sql.append(SQL_WHERE_CLAUSE);
        if (ProjectSortOrderEnum.FUNDING.equals(mSortOrder))
        {
            sql.append(SQL_ORDER_BY_BUDGET);
        }
        else
        {
            sql.append(SQL_ORDER_BY_PROJNUM);
        }
        return sql.toString();
    }

    /*
     * (non-javadoc)
     * @see edu.uwm.universitydb.framework.db.command.DBCommand#setParameters(java.sql.PreparedStatement)
     */
    @Override
    public void setParameters(final PreparedStatement pStmt) throws SQLException
    {
        pStmt.setString(1, mDeptId);

    }

}

/*
 * ============================================================================
 * Change History
 * ----------------------------------------------------------------------------
 * Apr 30, 2014   Shyam Nair
 * File created
 * ----------------------------------------------------------------------------
 */